package com.controller.backend;

import com.biz.BdTeamBiz;
import com.biz.BdUserinfoBiz;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.model.BdTeam;
import com.entity.model.BdUserinfo;
import com.util.PosterUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Condition;

import java.util.List;

/**
 * Created by jc on 2019/11/23.
 */
@Controller
@RequestMapping("/backend/security/bdTeam")
public class BdTeamController extends BaseController<BdTeam> {
    @Autowired
    private BdTeamBiz bdTeamBiz;

    @Autowired
    private BdUserinfoBiz bdUserinfoBiz;

    //地推长列表
    @RequestMapping(value = "index")
    public String index() throws Exception {
        return "/backend/security/bdTeam/list";
    }

    //地推详情页面
    @RequestMapping(value = "detail")
    public String detail() throws Exception {
        return "/backend/security/bdTeam/form";
    }

    @RequestMapping(value = "codeForm")
    public String codeForm() throws Exception {
        return "/backend/security/bdTeam/codeForm";
    }

    //新增地推
    @RequestMapping(value = "updateBdTeam", method = RequestMethod.POST)
    @ResponseBody
    public Result updateBdTeam(BdTeam bTeam) {
        if (bTeam != null) {
            Condition cond = new Condition(BdTeam.class);
            cond.createCriteria().andEqualTo("idCard", bTeam.getIdCard());
            List<BdTeam> list3 = bdTeamBiz.findByCondition(cond);
            if (list3.size() > 0 && list3.get(0).getId() != bTeam.getId()) {
                return ResultGenerator.genFailResult("该身份证已存在,请重新输入");
            } else {
                bdTeamBiz.update(bTeam);

                BdUserinfo bdUserinfo = bdUserinfoBiz.findBy("account", bTeam.getBackAccount());
                bdUserinfo.setPassword(bTeam.getPassword());
                bdUserinfoBiz.update(bdUserinfo);

                return ResultGenerator.genSuccessResult(bTeam);
            }
        } else {
            return ResultGenerator.genFailResult("操作失败");
        }
    }

    @RequestMapping(value = "addBdTeam", method = RequestMethod.POST)
    @ResponseBody
    public Result addBdTeam(BdTeam bTeam) {
        if (bTeam != null) {
            Condition cond = new Condition(BdTeam.class);
            cond.createCriteria().andEqualTo("account", bTeam.getAccount());
            Condition cond2 = new Condition(BdUserinfo.class);
            cond2.createCriteria().andEqualTo("account", bTeam.getBackAccount());
            Condition cond3 = new Condition(BdTeam.class);
            cond3.createCriteria().andEqualTo("idCard", bTeam.getIdCard());
            if (bdTeamBiz.countByCond(cond) > 0) {
                return ResultGenerator.genFailResult("该手机号已存在,请重新输入");
            }
            if (bdUserinfoBiz.countByCond(cond2) > 0) {
                return ResultGenerator.genFailResult("该后台账号已存在,请重新输入");
            }
            if (bdTeamBiz.countByCond(cond3) > 0) {
                return ResultGenerator.genFailResult("该身份证已存在,请重新输入");
            } else {

                bdTeamBiz.save(bTeam);

                BdUserinfo bdUserinfo = new BdUserinfo();
                bdUserinfo.setAccount(bTeam.getBackAccount());
                bdUserinfo.setPassword(bTeam.getPassword());
                bdUserinfo.setBdTeamId(bTeam.getId());
                bdUserinfo.setIsSuper((short) 1);
                bdUserinfo.setTelNo(bTeam.getAccount());
                bdUserinfo.setUserName(bTeam.getUsername());
                bdUserinfoBiz.save(bdUserinfo);

                return ResultGenerator.genSuccessResult(bTeam);
            }
        } else {
            return ResultGenerator.genFailResult("操作失败");
        }
    }

    @RequestMapping(value = "getQRcode")
    public Result getQRcode() {

        String url = PosterUtil.payQrPoster();
        if (url == null) {
            return ResultGenerator.genFailResult("生成二维码失败");
        }
        return ResultGenerator.genSuccessResult(url);
    }
}
